package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BasisCode;
import com.ruoyi.system.domain.BasisSign;
import com.ruoyi.system.domain.BasisStudent;
import com.ruoyi.system.service.IBasisSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 签到信息Controller
 *
 * @author ruoyi
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/system/sign")
public class BasisSignController extends BaseController
{
    @Autowired
    private IBasisSignService basisSignService;

    /**
     * 查询签到信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasisSign basisSign)
    {
        startPage();
        List<BasisSign> list = basisSignService.selectBasisSignList(basisSign);
        return getDataTable(list);
    }
    /**
     * 查询签到信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sign:lista')")
    @GetMapping("/lista")
    public TableDataInfo lista(BasisSign basisSign)
    {
        startPage();
        List<BasisSign> list = basisSignService.selectBasisSignLista(basisSign);
        return getDataTable(list);
    }

    /**
     * 查询签到信息可视化列表
     */
    @PreAuthorize("@ss.hasPermi('system:sign:listchart')")
    @GetMapping("/listchart")
    public AjaxResult listchart(BasisCode basisCode)
    {
        startPage();
        Integer count = basisSignService.selectBasisCharta(basisCode);

        List<BasisStudent> list = basisSignService.selectBasisChartb(basisCode);


        String[] arr=new String[list.size()];
        Integer[] arr1=new Integer[list.size()];
        Integer[] arr2=new Integer[list.size()];
        Integer[] arr3=new Integer[list.size()];

        for(int i=0;i<list.size();i++){
            BasisSign basisSign=new BasisSign();
            BasisStudent basisStudent=list.get(i);
            basisSign.setIdcard(basisStudent.getIdcard());
            //正常出席
            Integer countc = basisSignService.selectBasisChartc(basisSign);
            if(countc==null){
                countc=0;
            }
            //迟到
            Integer countd = basisSignService.selectBasisChartd(basisSign);
            if(countd==null){
                countd=0;
            }
            //缺席
            arr[i]= basisStudent.getStudentName();
            arr1[i]=countc;
            arr2[i]=countd;
            arr3[i]=count-countc-countd;
        }
        HashMap result=new HashMap();
        result.put("arr", (Arrays.toString(arr)).replace("[","").replace("]",""));
        result.put("arr1", (Arrays.toString(arr1)).replace("[","").replace("]",""));
        result.put("arr2", (Arrays.toString(arr2)).replace("[","").replace("]",""));
        result.put("arr3", (Arrays.toString(arr3)).replace("[","").replace("]",""));


        return AjaxResult.success(result);
    }



    @PreAuthorize("@ss.hasPermi('system:sign:listb')")
    @GetMapping("/listb")
    public TableDataInfo listb(BasisSign basisSign)
    {
        startPage();
        List<BasisSign> list = basisSignService.selectBasisSignListb(basisSign);
        return getDataTable(list);
    }

    /**
     * 导出签到信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sign:export')")
    @Log(title = "签到信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BasisSign basisSign)
    {
        List<BasisSign> list = basisSignService.selectBasisSignList(basisSign);
        ExcelUtil<BasisSign> util = new ExcelUtil<BasisSign>(BasisSign.class);
        return util.exportExcel(list, "签到信息数据");
    }

    /**
     * 获取签到信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sign:query')")
    @GetMapping(value = "/{signId}")
    public AjaxResult getInfo(@PathVariable("signId") Long signId)
    {
        return AjaxResult.success(basisSignService.selectBasisSignById(signId));
    }

    /**
     * 新增签到信息
     */
    @Log(title = "签到信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody BasisSign basisSign)
    {
        return toAjax(basisSignService.insertBasisSign(basisSign));
    }

    /**
     * 修改签到信息
     */
    @PreAuthorize("@ss.hasPermi('system:sign:edit')")
    @Log(title = "签到信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasisSign basisSign)
    {
        return toAjax(basisSignService.updateBasisSign(basisSign));
    }

    /**
     * 删除签到信息
     */
    @PreAuthorize("@ss.hasPermi('system:sign:remove')")
    @Log(title = "签到信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{signIds}")
    public AjaxResult remove(@PathVariable Long[] signIds)
    {
        return toAjax(basisSignService.deleteBasisSignByIds(signIds));
    }
}
